package com.lyun.controller;

import com.lyun.common.EveryConstant;
import com.lyun.common.JwtUtils;
import com.lyun.dto.LocationDto;
import io.jsonwebtoken.Claims;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/baidu")
public class BaiduController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 上传经纬度信息
     *
     * @param dto
     */
    @PostMapping("/location")
    public void uploadLocation(@RequestHeader("Authorization") String token,
                               @RequestBody LocationDto dto) {

        Claims claims = JwtUtils.getClaims(token);
        String id = claims.get("id").toString();

        //将用户所在的经纬度 存入redis
        redisTemplate.opsForGeo().add(EveryConstant.REDIS_GEO_USER, new Point(dto.getLongitude(), dto.getLatitude()), id);

        //将用户所在地存入redis
        redisTemplate.opsForValue().set(EveryConstant.REDIS_GEO_USER_LOCATION + id, dto.getAddrStr(), 1, TimeUnit.DAYS);

    }

}